<template>
  <div class="main">
    <el-table
      :data="tableData"
      style="width: 100%"
      :header-cell-style="{ backgroundColor: '#fafafa' }"
    >
      <el-table-column prop="number" label="试题编号" width="150" />
      <el-table-column prop="subject" label="学科" width="120" />
      <el-table-column prop="catalog" label="目录" width="120" />
      <el-table-column prop="questionType" label="题型" width="120" />
      <el-table-column label="题干" width="300">
        <template #default="{ row }">
          <span v-html="row.question"></span>
        </template>
      </el-table-column>
      <el-table-column prop="addDate" label="录入时间" width="120" />
      <el-table-column prop="difficulty" label="难度" width="120" />
      <el-table-column prop="creator" label="录入人" width="120" />
      <el-table-column prop="chkState" label="审核状态" width="120" />
      <el-table-column prop="chkRemarks" label="审核意见" width="120" />
      <el-table-column prop="chkUser" label="审核人" width="120" />
      <el-table-column prop="publishState" label="发布状态" width="120" />
      <el-table-column
        fixed="right"
        label="操作"
        width="200"
        header-align="center"
      >
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">
            预览
          </el-button>
          <el-button
            type="text"
            size="small"
            :disabled="scope.row.chkState === '1' ? false : true"
          >
            审核
          </el-button>
          <el-button
            type="text"
            size="small"
            :disabled="scope.row.pad ? true : false"
          >
            修改
          </el-button>
          <el-button type="text" size="small" @click="shelves(scope.row)">
            {{ scope.row.pad ? '下架' : '上架' }}
          </el-button>
          <el-button
            type="text"
            size="small"
            :disabled="scope.row.pad ? true : false"
            @click="delFn(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 对话框 -->
    <popUpLayer :arrAlr="arrAlr" :dialogVisible.sync="dialogVisible" />
  </div>
</template>
<script>
import popUpLayer from '@/views/nested/menu1/components/pop-upLayer/index.vue'
import { postQuestionsRandomsApi } from '@/api/menu'
export default {
  props: {
    tableData: {
      type: Array
    }
  },
  components: {
    popUpLayer
  },
  data() {
    return {
      dialogVisible: false,
      arrAlr: {},
      numberOnTheShelf: null // 存储上架下架的数字
    }
  },
  methods: {
    handleClick(row) {
      this.dialogVisible = true
      this.arrAlr = row
    },
    async shelves(row) {
      const idx = this.tableData.findIndex((item) => item.id === row.id)
      if (this.tableData[idx].pad) {
        this.$confirm('是否下架?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.$message({
              type: 'success',
              message: '下架成功!'
            })
            this.tableData[idx].pad = !this.tableData[idx].pad
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消下架'
            })
          })
      } else {
        this.$confirm('是否上架?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.$message({
              type: 'success',
              message: '上架成功!'
            })
            this.tableData[idx].pad = !this.tableData[idx].pad
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消上架'
            })
          })
      }
    },
    async delFn(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await postQuestionsRandomsApi(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.$emit('successEle')
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>
<style scoped lang="scss">
</style>
